package com.jackie.setting

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView

class NameSelectorActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_name_selector)

        val names = listOf(
            "A", "B", "C", "D", "E", "F",
            "G", "H", "I", "J", "K", "L",
            "M", "N", "O", "P", "Q", "R",
            "S", "T", "U", "V", "W", "X",
            "Y", "Z"
        )
        val selectedName = intent.getStringExtra(MainActivity.ARG_NAME)

        findViewById<RecyclerView>(R.id.recycler_view)
            .adapter = NameAdapter(this, names, selectedName)
            .also {
                it.onItemClickListener = { _, name, _ ->
                    val intent = Intent()
                    intent.putExtra(MainActivity.ARG_NAME, name)
                    setResult(Activity.RESULT_OK, intent)
                    finish()
                }
            }
    }

    companion object {
        fun showForResult(activity: Activity, name: String?, requestCode: Int) {
            val intent = Intent(activity, NameSelectorActivity::class.java)
            intent.putExtra(MainActivity.ARG_NAME, name)
            activity.startActivityForResult(intent, requestCode)
        }
    }
}